#!/bin/bash
#
# wuserver    COSYTEK IOT server
#
# chkconfig: 345 85 45
# description: IOT server is major for IOT service
# processname: wuserver

# Source function library.
. /etc/init.d/functions

RETVAL=0
prog="wuserver"
LOCKFILE=/var/lock/subsys/$prog

# Declare variables for wuserver
WUSERVER_DIR=/root/wswitch-server-dis/wuserver
WUSERVER=$WUSERVER_DIR/bin/wuserver
WUSERVER_CFG=/etc/wuserver/wuserver.conf

start() {
        if [ -f $LOCKFILE ]
        then
            echo "$LOCKFILE exists, process is already running or crashed"
            exit 1
        fi
        if [ ! -f $WUSERVER_CFG ]
        then
            echo "$WUSERVER_CFG not exists, need config file for wuserver"
            exit 1
        fi
        echo -n "Starting $prog: "
        daemon $WUSERVER -c $WUSERVER_CFG &
        RETVAL=$?
        [ $RETVAL -eq 0 ] && touch $LOCKFILE
        echo
        return $RETVAL
}

stop() {
        if [ ! -f $LOCKFILE ]
        then
            echo "$LOCKFILE does not exist, process is not running"
            exit 1
        fi
        echo -n "Shutting down $prog: "
        killproc $prog -TERM
        RETVAL=$?
        [ $RETVAL -eq 0 ] && rm -f $LOCKFILE
        echo
        return $RETVAL
}

status() {
        echo -n "Checking $prog status: "
        if [ -f $LOCKFILE ]
        then
            echo "process is running"
            exit 0
        else
            echo "process is not running"
            exit 0
        fi
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status
        ;;
    restart)
        stop
        start
        ;;
    *)
        echo "Usage: $prog {start|stop|status|restart}"
        exit 1
        ;;
esac
exit $RETVAL